# encoding: utf-8
""" 
@version: v1.0 
@author: autumner 
@license: Apache Licence  
@contact: 18322313385@163.com 
@site:  https://gitee.com/autumner/pythoncookbook
@software: PyCharm 
@file: net_simplexmlrpcserver_server.py 
@time: 2019/7/15 下午2:21
"""
'''
SimpleXMLRPCServer的使用,服务端
'''
from xmlrpc.server import SimpleXMLRPCServer
from xmlrpc.server import SimpleXMLRPCRequestHandler

# Restrict to a particular path.
class RequestHandler(SimpleXMLRPCRequestHandler):
    rpc_paths = ('/RPC2')

# creater server
with SimpleXMLRPCServer(('localhost', 8000),
                        requestHandler= RequestHandler) as server:
    server.register_introspection_functions()
    # 注册内置函数
    server.register_function(pow)
    # 注册自定义函数
    def adder_function(x, y):
        return x + y
    server.register_function(adder_function, 'add')
    # 使用装饰器注册自定义函数
    @server.register_function(name='sub')
    def sub_function(x, y):
        return x - y
    # 注册一个实例，实例所有方法作为xml-rpc方法发布
    class MyFuncs:
        def mul(self, x, y):
            return x * y

    server.register_instance(MyFuncs())

    # 使用匿名函数注册作为XML-RPC方法发布
    server.register_function(lambda x, y: x / y, 'div')

    # Run the server's main loop
    server.serve_forever()